Micron Document




Factory method pattern
part 11/15 · 25.1 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
self.connected_rooms.append(room)
class MagicRoom(Room):
def __str__(self) -> str:
return "Magic room"
class OrdinaryRoom(Room):
def __str__(self) -> str:
return "Ordinary room"
ordinaryGame = OrdinaryMazeGame()
ordinaryGame.play()
magicGame = MagicMazeGame()
magicGame.play()

Uses

• In ADO.NET, IDbCommand.CreateParameter is an example of the use of factory method to connect parallel class hierarchies.
• In Qt, QMainWindow::createPopupMenu Archived 2015-07-19 at the Wayback Machine is a factory method declared in a framework that can be overridden in application code.
• In Java, several factories are used in the javax.xml.parsers package, such as javax.xml.parsers.DocumentBuilderFactory or javax.xml.parsers.SAXParserFactory.
• In the HTML5 DOM API, the Document interface contains a createElement() factory method for creating specific elements of the HTMLElement interface.

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────